/**
 * [ajaxGetData ajax请求页面数据的函数]
 * @param  {[type]} data [对象]
 * @return {[type]}      [description]
 */
	
function ajaxGetData(data) //{url:inddd.jlll}
{
	var defaultData = {
		url  : 'look.html',
		data : {page:1},
		dataType:'json',
		element : '.number-table',
		dataType:'html',
		type:'get',
	},
	setting;
	setting = $.extend(defaultData,data);

	$.ajax({
		url  : setting.url,
		data : setting.data,
		type : setting.type,
		dataType :setting.dataType,
		async : false,
		success:function(data){
			// console.log(data);
			$(setting.element).html(data);
		}
	})
}

//点击添加分类按钮跳出模态框
$('.add_classify').click(function(){
	$('.classifyadd').css('display','block');
});

//添加分类点击确认
$('.tianjia').click(function(){
	$('.tianjia').attr('disabled',true);
	var waibu = sessionStorage.getItem('kehushow');
	if(waibu == 0){
		var type = 1;	
	}else{
		var type = 2;
	}
	var name = $('.fenlei').val();
	if(name == '' || name.length > 6){
		alert('必须填写分类名并长度不能大于6位');
		$('.tianjia').attr('disabled',false);
		return false;
	}else{
		$.ajax({
		type:'get',
		url:'add.html',
		data:{'name':name,'type':type},
		success:function(data){	
			data = eval('('+data+')');			
			$('.classifyadd').css('display','none');
			if(waibu == 0){				
				$('.xiugaifenlei .add-js').before("<li><a class='addcolor' data-id='"+data.id+"'><span class='fenleiname'>"+data.name+"</span><span class='number' data-id='"+data.id+"'>(0)</span></a><a class='xiugai-a' data-id='"+data.id+"'><img src='"+img_host+"/images/head/bi.png' /></a></li>");
				$('.renxuanshoucang .move-list').append("<li data-id='"+data.id+"'>"+data.name+"</li>")
			}else{
				$('.xiugaifenlei1 .add-js').before("<li><a class='addcolor1' data-id='"+data.id+"'><span class='fenleiname1' data-id='"+data.id+"'>"+data.name+"</span><span class='number1' data-id='"+data.id+"'>(0)</span></a><a class='xiugai-a' data-id='"+data.id+"'><img src='"+img_host+"/images/head/bi.png' /></a></li>");
				$('.waibushoucang .move-list').append("<li data-id='"+data.id+"'>"+data.name+"</li>")
			}
			$('.tianjia').attr('disabled',false);
		}
		});
	}
});

//修改收藏分类
var id = '';
var index = '';
$('.xiugaifenlei').delegate('.xiugai-a','click',function(){
	id = $(this).attr('data-id');
	// alert(id);
	index = $(this).parent().index()-2;
	// alert(index);
	$('.xiugai-js').css('display','block');
})

//修改外部人选分类
$('.xiugaifenlei1').delegate('.xiugai-a','click',function(){
	id = $(this).attr('data-id');
	// alert(id);
	index = $(this).parent().index()-2;
	// alert(index);
	$('.xiugai-js').css('display','block');
})

//修改分类点击确认
$('.rename').click(function(){
	var waibu = sessionStorage.getItem('kehushow');
	var newname = $('.newname').val();
	if(newname == '' || newname.length > 6){
		alert('必须填写名称并长度不能大于6位');
		return false;
	}else{
		$.ajax({
			type:'get',
			url:'xiugai.html',
			data:{'newname':newname,'id':id},
			success:function(data){	
				data = eval('('+data+')');
				$('.xiugai-js').css('display','none');
				if(waibu == 0){
					//选中的是收藏人选
					$('.fenleiname').eq(index).html(data.name);
					$('.move-list li').eq(index).html(data.name);
				}else{
					//选中的是外部人选
					$('.fenleiname1').eq(index).html(data.name);
					$('.waibushoucang .move-list li').eq(index).html(data.name);
				}			
			}
		});
	}
});

//删除分类
$('.deletefenlei').click(function(){
	$('.xiugai-js').css('display','none');
	$('.shanchu').css('display','block');
});

//删除分类点击确认
$('.shanchusure').click(function(){
	var waibu = sessionStorage.getItem('kehushow');
	if(waibu == 0){
		//删除收藏人选中的分类
			$.ajax({
			type:'get',
			url:'delete.html',
			data:{'id':id},
			success:function(data){	
				data = eval('('+data+')');
				if(data.code == 200){
					//删除分类把分类里面的人选变成未分类
					$('.xiugaifenlei li').eq(index+2).remove();
					$('.shanchu').css('display','none');
					$('.move-list li').eq(index).remove();
					var number = $('.number').filter(function(index){
						return $(this).attr('data-id') == 0;
					});
					var count = /\((\d+)\)/.exec(number.text())[1];
					// console.log(/\((\d+)\)/.exec(number.text())[1]);
					number.html('('+(parseInt(count)+data.number)+')');
					//重新请求数据
					ajaxGetData({
						url  : 'get_group_owner.html',
						data : {'group_id':-1},
						dataType:'html',
						element : '.renxuanshoucang',
						dataType:'html',
					});
					$('.xiugaifenlei .addcolor').css('color','');
					$('.xiugaifenlei .addcolor').eq(0).css('color','#53B2FB');
				}
			}
		});
	}else{
		//删除外部人选中的分类
			$.ajax({
			type:'get',
			url:'deletewaibu.html',
			data:{'id':id},
			success:function(data){	
				data = eval('('+data+')');
					if(data.code == 200){
						//删除分类把分类里面的人选变成未分类
						$('.xiugaifenlei1 li').eq(index+2).remove();
						$('.shanchu').css('display','none');
						$('.waibushoucang .move-list li').eq(index).remove();
						var number = $('.number1').filter(function(index){
							return $(this).attr('data-id') == 0;
						});
						var count = /\((\d+)\)/.exec(number.text())[1];
						// console.log(/\((\d+)\)/.exec(number.text())[1]);
						number.html('('+(parseInt(count)+data.number)+')');
						//重新外部人选请求数据
						ajaxGetData({
							url  : 'get_ex_owner.html',
							data : {'group_id':-1},
							dataType:'html',
							element : '.waibushoucang',
							dataType:'html',
						});
						$('.xiugaifenlei1 .addcolor1').css('color','');
						$('.xiugaifenlei1 .addcolor1').eq(0).css('color','#53B2FB');
						}
				}
			});
		 }
	
});

//所有分类击分页按钮ajax分页查询数据
$('.renxuanshoucang').delegate('.pagination a','click',function(event){
	//点击分页按钮 进行ajax分页

	//阻止默认事件
	var e = window.event || event;
	var	page = $(this).data('page');

	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.returnValue = false;
	};
	sessionStorage.setItem("page", page);
	var group_id = sessionStorage.getItem('group_id');
	//请求数据
	ajaxGetData({
		url  : 'get_group_owner.html',
		data : {'group_id':group_id,'page':page+1},
		dataType:'html',
		element : '.renxuanshoucang',
		dataType:'html',
	});

}).delegate('.quxiaoshoucang','click',function(){
	var index = $(this).index();
	//我的人选取消收藏或收藏
	var owner_id = $(this).attr('data-id');
	// alert(owner_id);
	$.ajax({
		type:'get',
		url:'quxiaoshoucang.html',
		data:{'owner_id':owner_id},
		dataType:'json',
		// async:false,
		success:function(data){
			if(data.code == 200){
				// console.log(data.c_id.classify_id);
				//计算本页li的个数，如果是1的话说明只有一条数据，那么跳到上一页
				var length = $('.renxuanshoucang li').length;
				$('.renxuanshoucang li').eq(index).remove();
				var group_id = sessionStorage.getItem('group_id');
				//全部数量-1
				var number = $('.number').filter(function(index){
					return $(this).attr('data-id') == -1;
				});
				var count = /\((\d+)\)/.exec(number.text())[1];
				// console.log(/\((\d+)\)/.exec(number.text())[1]);
				number.html('('+(parseInt(count)-1)+')');
				//删除数据属于的分类数量-1
				if(!data.c_id){
					// console.log('0');
					// console.log(data.classify_id);
					var number1 = $('.number').filter(function(index){
						return $(this).attr('data-id') == 0;
					});
					var count1 = /\((\-?\d+)\)/.exec(number1.text())[1];
					number1.html('('+(parseInt(count1)-1)+')');
				}else{
					var classify_id = data.c_id.classify_id;
					var number1 = $('.number').filter(function(index){
						return $(this).attr('data-id') == classify_id;
					});
					// console.log('1');
					// console.log('classify_id:'+classify_id)
					// console.log('number1:'+number1.text());
					// console.log(/\((\d+)\)/.exec(number1.text()));
					var count1 = /\((\-?\d+)\)/.exec(number1.text())[1];
					number1.html('('+(parseInt(count1)-1)+')');
				}
				// console.log(sessionStorage.getItem('page'));
				// console.log(sessionStorage.getItem('group_id'));
				if(length == 1){
					var page = sessionStorage.getItem('page')-1;
					if(page == -1){
					var	page = 0;
					}
				}else{
					var page = sessionStorage.getItem('page');
				}
				ajaxGetData({
					url  : 'get_group_owner.html',
					data : {'group_id':sessionStorage.getItem('group_id'),'page':sessionStorage.getItem('page')+1},
					dataType:'html',
					element : '.renxuanshoucang',
					dataType:'html',
				});
			}
		}
	});
});
sessionStorage.setItem('group_id',-1);
$('.xiugaifenlei .addcolor').eq(0).css('color','#53B2FB');
$('.xiugaifenlei1 .addcolor1').eq(0).css('color','#53B2FB');
sessionStorage.setItem('page',0);
//点击收藏下面的分类按钮选项卡选中后变色以及查询数据
$('.xiugaifenlei').delegate('.addcolor','click',function(){
	$('.xiugaifenlei .addcolor').css('color','');
	$(this).css('color','#53B2FB');
	var id = $(this).attr('data-id');
	sessionStorage.setItem('page',0);
	sessionStorage.setItem('group_id',id);
	ajaxGetData({
		url  : 'get_group_owner.html',
		data : {'group_id':id},
		dataType:'html',
		element : '.renxuanshoucang',
		dataType:'html',
	});
})

//收藏人选点击分类
$('.renxuanshoucang').delegate('.move-list li','click',function(){
	var owner_id = $(this).parent().attr('data-id');
	//当前人选属于的当前分组id
	var prev_id = $(this).parent().prev().attr('data-id');
	//移动至哪个分类id
	var g_id = $(this).attr('data-id');
	//当前点击的是哪个分类id
	var oldg_id = sessionStorage.getItem('group_id');
	var page = sessionStorage.getItem('page');
	// alert(oldg_id);
	$.ajax({
		type:'get',
		url:'fenlei.html',
		data:{'owner_id':owner_id,'g_id':g_id,'prev_id':prev_id},
		dataType:'json',
		success:function(data){
			if(data.code == 200){
				//分组成功重新ajax请求组内人选信息
				ajaxGetData({
					url  : 'get_group_owner.html',
					data : {'group_id':oldg_id,'page':page+1},
					dataType:'html',
					element : '.renxuanshoucang',
					dataType:'html',
				});
				//被移动的分组数量-1					
				var number1 = $('.number').filter(function(index){
					return $(this).attr('data-id') == prev_id;
				});
				var count1 = /\((\-?\d+)\)/.exec(number1.text())[1];
				number1.html('('+(parseInt(count1)-1)+')');
				//移动至的分组数量+1
				var number2 = $('.number').filter(function(index){
					return $(this).attr('data-id') == g_id;
				});
				var count2 = /\((\-?\d+)\)/.exec(number2.text())[1];
				number2.html('('+(parseInt(count2)+1)+')');
			}
		}
	})
})


//外部人选点击移动分类
$('.waibushoucang').delegate('.move-list li','click',function(){
	var exowner_id = $(this).parent().attr('data-id');
	//当前人选属于的当前分组id
	var prev_id = $(this).parent().prev().attr('data-id');
	//移动至哪个分类id
	var g_id = $(this).attr('data-id');
	//当前点击的是哪个分类id
	var oldg_id = sessionStorage.getItem('group_id');
	var page = sessionStorage.getItem('page');
	// alert(oldg_id);
	$.ajax({
		type:'get',
		url:'waibufenlei.html',
		data:{'exowner_id':exowner_id,'g_id':g_id,'prev_id':prev_id},
		dataType:'json',
		success:function(data){
			if(data.code == 200){
				//分组成功重新ajax请求组内人选信息
				ajaxGetData({
					url  : 'get_ex_owner.html',
					data : {'group_id':oldg_id,'page':page+1},
					dataType:'html',
					element : '.waibushoucang',
					dataType:'html',
				});
				//被移动的分组数量-1					
				var number1 = $('.number1').filter(function(index){
					return $(this).attr('data-id') == prev_id;
				});
				var count1 = /\((\-?\d+)\)/.exec(number1.text())[1];
				number1.html('('+(parseInt(count1)-1)+')');
				//移动至的分组数量+1
				var number2 = $('.number1').filter(function(index){
					return $(this).attr('data-id') == g_id;
				});
				var count2 = /\((\-?\d+)\)/.exec(number2.text())[1];
				number2.html('('+(parseInt(count2)+1)+')');
			}
		}
	})
})


//点击推荐职位
$('.renxuanshoucang').delegate('.tuiJ','click',function(event){
	var owner_id = $(this).attr('data-id');
	var has_recommend_authority = $('.has_recommend_authority').val();
	var e = window.event || event;
	// var head_id = $(this).attr('head_id');
	if(has_recommend_authority == 1){
		$.ajax({
			url:web_url+'/home/candidate/tuijian.html',
			type:'get',
			dataType:'json',
			data:{'owner_id':owner_id},
			success:function(data){
				if(data.code == 400){
					$('.renzheng').css('display','block');
				};
				if(data.code == 200){
					window.location.href = web_url+'/home/candidate/tjdetails.html?owner_id='+owner_id;
				}
			}
		})
	}else{
		if(e.preventDefault){
			e.preventDefault();
		}else{
			e.returnValue = false;
		};
		alert('对不起,您暂时没有推荐权限');
	}
	
})


//点击外部人选默认查询
$('.outrenxuan').click(function(){
	// alert(111);
	sessionStorage.setItem("page", 0);
	ajaxGetData({
		url  : 'get_ex_owner.html',
		data : {'group_id':-1},
		dataType:'html',
		element : '.waibushoucang',
		dataType:'html',
	});
});

//外部人选点击分类查询人选
$('.xiugaifenlei1').delegate('.addcolor1','click',function(){
	$('.xiugaifenlei1 .addcolor1').css('color','');
	$(this).css('color','#53B2FB');
	var id = $(this).attr('data-id');
	sessionStorage.setItem('page',0);
	sessionStorage.setItem('group_id',id);
	ajaxGetData({
		url  : 'get_ex_owner.html',
		data : {'group_id':id},
		dataType:'html',
		element : '.waibushoucang',
		dataType:'html',
	});
})

//外部人选ajax点击分页
$('.waibushoucang').delegate('.pagination a','click',function(event){
	//阻止默认事件
	var e = window.event || event;
	var	page = $(this).data('page');

	if(e.preventDefault){
		e.preventDefault();
	}else{
		e.returnValue = false;
	};
	sessionStorage.setItem("page", page);
	var id = sessionStorage.getItem('group_id');
	ajaxGetData({
		url  : 'get_ex_owner.html',
		data : {'group_id':id,'page':page+1},
		dataType:'html',
		element : '.waibushoucang',
		dataType:'html',
	});
})


//外部人选删除
$('.waibushoucang').delegate('.deleteexo','click',function(){
	var index = $(this).index();
	var exowner_id = $(this).attr('data-id');
	var class_id = $(this).attr('class-id');
	$.ajax({
		url:'deleteexo.html',
		type:'get',
		dataType:'json',
		data:{'exowner_id':exowner_id},
		success:function(data){
			if(data.code == 200){
				// console.log(data.c_id.classify_id);
				//计算本页li的个数，如果是1的话说明只有一条数据，那么跳到上一页
				var length = $('.waibushoucang li').length;
				$('.waibushoucang li').eq(index).remove();
				var group_id = sessionStorage.getItem('group_id');
				//全部数量-1
				var number = $('.number1').filter(function(index){
					return $(this).attr('data-id') == -1;
				});
				var count = /\((\d+)\)/.exec(number.text())[1];
				// console.log(/\((\d+)\)/.exec(number.text())[1]);
				number.html('('+(parseInt(count)-1)+')');
				//删除数据属于的分类数量-1
				var number1 = $('.number1').filter(function(index){
					return $(this).attr('data-id') == class_id;
				});
				var count1 = /\((\-?\d+)\)/.exec(number1.text())[1];
				number1.html('('+(parseInt(count1)-1)+')');
				//通过页面上有几条数据判断删除后跳转至当页还是上一页
				if(length == 1){
					var page = sessionStorage.getItem('page')-1;
					if(page == -1){
					var	page = 0;
					}
				}else{
					var page = sessionStorage.getItem('page');
				}
				ajaxGetData({
					url  : 'get_ex_owner.html',
					data : {'group_id':group_id,'page':page+1},
					dataType:'html',
					element : '.waibushoucang',
					dataType:'html',
				});
			}
		}
	})
})


